<script>
import Vue from 'vue';
export default {
	//简单全局变量  =>getApp().globalData.text = 'test'
	globalData: {
		// text: 'text'
		flag: false
	},
	onLaunch: function() {
		console.log('平台', this.$u.os());
		// 获取系统状态栏的高度
		const token = uni.getStorageSync('token');
		// console.log('token', token);
		if (token) {
			uni.switchTab({
				url: '/pages/index/index?isone=1'
			});
		}
		uni.getSystemInfo({
			success: function(e) {
				Vue.prototype.phoneWidth = e.windowWidth; //手机宽度
				Vue.prototype.statusBarHeight = e.statusBarHeight; //系统顶部状态栏高度
				console.log(e.statusBarHeight);
			}
		});
	},
	onShow: function() {
		var args = plus.runtime.arguments;
		// let args =
		// 	'letuiApp://x5sec?url=https%3A%2F%2Fpub.alimama.com%2F%2Fcp%2Fevent%2Fitem%2Flist.json%2F%5F%5F%5F%5F%5Ftmd%5F%5F%5F%5F%5F%2Fpunish%3Fx5secdata%3D5e0c8e3884010a5%26x5step%3D2%26ali%3D18';
		if (args) {
			let urlstr = decodeURIComponent(args);
			console.log('args', urlstr);
			// hbuilder://
			const i = urlstr.indexOf('?');
			if (i !== -1) {
				var ustr = urlstr.slice(i, urlstr.length);
				var url = urlstr.slice(i + 5, urlstr.length);
				var name = 'ali';
				var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
				var e = ustr.substr(1).match(reg);
				if (e != null) {
					var ali = unescape(e[2]); //获取  ali 参数
					console.log('ali', unescape(e[2]));
				} else {
					console.log('ali 为 null');
				}
				var w = plus.webview.create(url);
				w.show();
				var inter = setInterval(() => {
					let cookie = plus.navigator.getCookie('https://pub.alimama.com');
					if (cookie) {
						let cookieObj = JSON.parse(cookie);
						if (cookieObj.x5sec) {
							this.$u.api
								.dosetCookie({
									x5sec: cookieObj.x5sec,
									ali: ali
								})
								.then(res => {
									console.log(res);
									clearInterval(inter); // 清除定时器
									plus.navigator.removeCookie('https://pub.alimama.com'); //删除cookie
									w.close(); //关闭并销毁
								})
								.catch(err => {
									console.log(err);
								});
						}
					} else {
						clearInterval(inter); // 清除定时器
						console.log('cookie为空');
					}
				}, 400);
			}
		}
	},
	onHide: function() {
		console.log('App Hide');
	}
};
</script>

<style lang="scss">
@import 'uview-ui/index.scss';
@import './static/iconfont-weapp-icon.css';

/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
.status_bar {
	height: var(--status-bar-height);
	width: 100%;
	background-color: #ffffff;
}

.top_view {
	height: var(--status-bar-height);
	width: 100%;
	position: fixed;
	background-color: #ffffff;
	top: 0;
	z-index: 999;
}

.title-padding {
	height: var(--status-bar-height);
	padding-top: var(--status-bar-height);
	// background-color: #ff00ff;
	box-sizing: content-box;
}

/* 解决头条小程序组件内引入字体不生效的问题 */
/* #ifdef MP-TOUTIAO */
@font-face {
	font-family: uniicons;
	src: url('/static/uni.ttf');
}

/* #endif */

@font-face {
	font-family: iconfont;
	font-weight: normal;
	font-style: normal;
	src: url('http://at.alicdn.com/t/font_2135327_m38bk3mcxo.ttf') format('truetype');
}

.iconfont {
	font-family: 'iconfont' !important;
	font-size: 32rpx;
	font-style: normal;
	-webkit-font-smoothing: antialiased;
	-moz-osx-font-smoothing: grayscale;
}

.icon-copyt:before {
	content: '\e600';
}
.icon-download:before {
	content: '\e7bd';
}
.icon-copy:before {
	content: '\e67b';
}
.icon-money:before {
	content: '\e603';
}
.icon-search:before {
	content: '\e718';
}
</style>
